package com.cardmanager.pro.business.impl;

import java.math.BigDecimal;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cardmanager.pro.business.CreditCardAccountHistoryBusiness;
import com.cardmanager.pro.pojo.CreditCardAccountHistory;
import com.cardmanager.pro.repository.CreditCardAccountHistoryRepository;
@Service
public class CreditCardAccountHistoryBusinessImpl implements CreditCardAccountHistoryBusiness {

	@Autowired
	private CreditCardAccountHistoryRepository creditCardAccountHistoryRepository;
	
	@Autowired
	private EntityManager em;

	@Transactional
	@Override
	public CreditCardAccountHistory save(CreditCardAccountHistory creditCardAccountHistory) {
		creditCardAccountHistory = creditCardAccountHistoryRepository.save(creditCardAccountHistory);
		em.flush();
		em.clear();
		return creditCardAccountHistory;
	}

	/**
	 * 保存数据到交易历史记录表中
	 * @param addOrSub
	 * @param amount
	 * @param taskId
	 * @param creditCardAccountId
	 * @param blance
	 * @param description
	 */
	@Transactional
	@Override
	public void createNewHistory(int addOrSub, BigDecimal amount, String taskId, Long creditCardAccountId,
			BigDecimal blance, String description) {
		CreditCardAccountHistory creditCardAccountHistory = new CreditCardAccountHistory();
		creditCardAccountHistory.setAddOrSub(addOrSub);
		creditCardAccountHistory.setAmount(amount);
		creditCardAccountHistory.setTaskId(taskId);;
		creditCardAccountHistory.setCreditCardAccountId(creditCardAccountId);
		creditCardAccountHistory.setSumBlance(blance);
		creditCardAccountHistory.setDescription(description);
		creditCardAccountHistoryRepository.save(creditCardAccountHistory);
	}
}
